---
title: "Perceptual Possibiliites"
author: "Jonathan Phillips"
date: "2025-02-18"
output: html_document
---

```{r setup, include=FALSE}
knitr::opts_chunk$set(echo = FALSE,message = FALSE,warning = FALSE)

library(tidyverse)
library(lme4)
library(emmeans)
```

# Study 1

## Data cleaning

```{r 1 Cleaning}

d1 <- read.csv("Study1_CombinedData.csv")

d1 <- d1 |> mutate(
  block = as.numeric(substr(randomBlock,1,2)),
  targetType = case_when(
    grepl("AnaMatch",target) ~ "Unlikely",
    grepl("LocMatch",target) ~ "Unlikely",
    grepl("GloMatch",target) ~ "Likely",
    T ~ "noMatch"
  ),
  targetType = factor(targetType),
  primeShape = case_when(
    grepl("burst",prime) ~ "burst",
    grepl("star",prime) ~ "star",
    grepl("cross",prime) ~ "cross",
   T ~ "noPrime"
  ),
 primeShape = factor(primeShape),
 primeType = case_when(
   condition1=="simple" ~ "Amodal",
   condition1 == "anomolous"  ~ "Unlikely",
   condition1 == "local"  ~ "Unlikely",
   condition1 == "global" ~ "Likely",
   T ~ condition1
 ),
 primeType = factor(primeType)
)

d1.training <- d1 |>
  filter(condition1 == "familiarityTest" | condition1 == "matchPractice")

d1 <- d1 |>
  filter(condition1 != "familiarityTest", condition1 != "matchPractice")

d1.scores <- d1 |>
  group_by(id) |>
  summarize(score = mean(correct))

# quantile(d1$RT, probs = seq(0, 1, 0.025)) ## 95% = 1344.05; 97.5% = 1746.15

high1 <- 1746.15
#high1 <- 1500

remove <- subset(d1.scores, subset = (d1.scores$score < 0.9)) # create a subset of participants all under score threshold
#remove them from cleaned dataset
d1.cleaning <- d1[!(d1$id %in% remove$id),]

## remove any trials from the data wherein the participant responded faster than 300 ms or slower than 1500
d1.cleaning <- d1 |>
 filter(RT > 300, RT < high1)

for (d in unique(d1$id)){ ## loop over each participant
  # print(d)
  # print(nrow(d1.cleaning[d1.cleaning$id == d,]))
  # print(nrow(d1[d1$id == d,]))
  orig <- nrow(d1[d1$id == d,])
  clean <- nrow(d1.cleaning[d1.cleaning$id == d,])
  
  if(((orig - clean)/orig) > 0.1){ ## check and see if we removed trials from a given participant
    # print("hit")
    d1.cleaning <- d1.cleaning[d1.cleaning$id != d,] ## remove participant below the threshold
  }
}

```

## Demographics
```{r 1 demographics}

d1demo.clean <- d1.cleaning |>
  group_by(id) |>
  summarize(sex = unique(sex), age = unique(age))
# 
# length(unique(d1demo.clean$id)) # 51 participants
# mean(d1demo.clean$age) # Mean age = 39.84314
# sd(d1demo.clean$age) # SD age = 11.56438
# length(unique(d1demo.clean$id[d1demo.clean$sex == "female"])) # 23 identify as female

```

## Graph
```{r 1 graph}

d1.analysis <- d1.cleaning |>
  filter(correct==1, targetType !="noMatch", primeType != "noPrime") |>
  mutate(primeCat=case_when(
    primeType == "Likely" & targetType=="Likely" ~ "Congruent",
    primeType == "Unlikely" & targetType=="Unlikely" ~ "Congruent",
    primeType == "Likely" & targetType=="Unlikely" ~ "Incongruent",
    primeType == "Unlikely" & targetType=="Likely" ~ "Incongruent",
    primeType=="Amodal" ~ "Amodal"),
    primeCat = factor(primeCat, levels=c("Congruent","Amodal","Incongruent")),
    logRT = log(RT)
  )

# simple analysis and graphing approach:

d1.idAvgs <- d1.analysis |>  
group_by(id,primeCat) |>
  summarize(logRTavg = mean(logRT))


d1.sums <- d1.idAvgs|> group_by(primeCat) |>
  summarize(n=length(logRTavg),
            sd=sd(logRTavg,na.rm=T),
            se=sd/sqrt(n),
            logRTavg = mean(logRTavg)) 


study1fig <- ggplot(data=d1.idAvgs, aes(x=primeCat,y=logRTavg, color=primeCat, fill=primeCat)) +
  geom_jitter(alpha=.25) +
  geom_violin(alpha=.5)+
  #geom_line(aes(group=id)) +
  geom_errorbar(data=d1.sums, aes(ymin=logRTavg-1.96*se, ymax=logRTavg+1.96*se),width=.5) +
  geom_point(data=d1.sums, color="Black") +
  labs(y="Log(RT)",x="Prime Type",color="Prime Type") +
  theme_bw() +
  theme(plot.background = element_blank()
        ,panel.grid.major = element_blank()
        ,panel.grid.minor = element_blank()
        ,legend.text=element_text(size=rel(1.4))
        ,legend.position = "none",
        ,legend.title=element_text(size=rel(1.4))  
        ,axis.text.y=element_text(size=rel(1.4))
        ,axis.title.y=element_text(size=rel(1.3),vjust=.9)
        ,axis.title.x=element_text(size=rel(1.8))
        ,axis.text.x=element_text(size=rel(1.5))
        ,strip.text.x = element_text(size = rel(1.5))
  )

study1fig

ggsave(study1fig,file="../figs/study1.jpg",dpi = 700)

```

## Aanlyses

```{r d1 analyses}

lm1.0 <- lmer(logRT ~ primeCat + primeShape + (primeCat|id), data = d1.analysis)
lm1.1 <- lmer(logRT ~ primeShape + (primeCat|id), data = d1.analysis)
lm1.primeCat <- anova(lm1.0, lm1.1)
lm1.primeCat 

emm1 <- emmeans(lm1.0, specs = pairwise ~ primeCat,pbkrtest.limit = 7000)

emm1

```

# Study 2a

## Data cleaning

```{r 2a Cleaning}

d2a <- read.csv("Study2a_CombinedData.csv")

d2a <- d2a |> mutate(
  targetShape = case_when(
    grepl("AnaMatch",stimList) ~ "Unlikely",
    grepl("LocMatch",stimList) ~ "Unlikely",
    grepl("GloMatch",stimList) ~ "Likely",
    T ~ "noMatch"
  ),
  targetShape = factor(targetShape),
  primeShape = case_when(
    grepl("burst",stimList) ~ "burst",
    grepl("star",stimList) ~ "star",
    grepl("cross",stimList) ~ "cross",
   T ~ "noPrime"
  ),
 primeShape = factor(primeShape),
 primeType = case_when(
   primeType=="likely" ~ "Likely",
   primeType=="unlikely" ~ "Unlikely",
   primeType=="modal" ~ "Amodal",
   T ~ primeType
   )
)

d2a.training <- d2a |>
  filter(primeType == "familiarityTest" | primeType == "matchPractice", !is.na(id))

d2a.WM <- d2a |>
  filter(primeType == "probeWM", !is.na(id))

d2a <- d2a |>
  filter(primeType != "probeWM",primeType != "familiarityTest",
         primeType != "matchPractice", !is.na(id))


d2a.scores <- d2a |>
  group_by(id) |>
  summarize(score = mean(correct))

d2a.scoresWM <- d2a.WM |>
  group_by(id,load) |>
  summarize(score = mean(correct))

# median(d2a.scoresWM$score) # 0.8888889

remove <- subset(d2a.scores, subset = (d2a.scores$score < 0.9)) # create a subset of participants all under score threshold
remove <- bind_rows(remove, subset(d2a.scoresWM, subset = (d2a.scoresWM$score<0.85 & d2a.scoresWM$load=="low")))

#remove them from cleaned dataset
d2a.cleaning <- d2a[!(d2a$id %in% remove$id),]
d2a.cleaningWM <- d2a.WM[!(d2a.WM$id %in% remove$id),]

#quantile(d2a$RT, probs = seq(0, 1, 0.025)) ## 95% = 1855.000; 97.5% = 2256.125

high2a <- 2256.125
#high2a <- 2000  

## remove any trials from the data wherein the participant responded faster than 300 ms or slower than 2000
d2a.cleaning <- d2a.cleaning |>
  dplyr::filter(RT > 300, RT < high2a)

for (d in unique(d2a$id)){ ## loop over each participant
  # print(d)
  # print(nrow(d2a.cleaning[d2a.cleaning$id == d,]))
  # print(nrow(d2a[d2a$id == d,]))
  orig <- nrow(d2a[d2a$id == d,])
  clean <- nrow(d2a.cleaning[d2a.cleaning$id == d,])

  if((((orig - clean)/orig) > 0.1)){ ## check and see if we removed more than 10% of trials from a given participant
    #print("hit")
    d2a.cleaning <- d2a.cleaning[d2a.cleaning$id != d,] ## remove participant below the exclusion threshold
  }
}


```

## Demographics
```{r 2a demographics}

d2ademo.clean <- d2a.cleaning |>
  group_by(id) |>
  summarize(sex = unique(sex), age = unique(age))

# length(unique(d2ademo.clean$id)) # 82 participants
# mean(d2ademo.clean$age) # Mean age = 40.31707
# sd(d2ademo.clean$age) # SD age = 10.27628
# length(unique(d2ademo.clean$id[d2ademo.clean$sex == "female"])) # 33 identify as female

```

## Graph
```{r 2a graph}

d2a.analysis <- d2a.cleaning |>
  filter(correct==1, targetType=="match") |>
  mutate(
   PrimeCongruency = case_when(
    primeType == "Likely" & targetShape=="Likely" ~ "Congruent",
    primeType == "Unlikely" & targetShape=="Unlikely" ~ "Congruent",
    primeType == "Likely" & targetShape=="Unlikely" ~ "Incongruent",
    primeType == "Unlikely" & targetShape=="Likely" ~ "Incongruent",
    primeType=="Amodal" ~ "Amodal"),
   PrimeCongruency = factor(PrimeCongruency, levels=c("Congruent","Amodal","Incongruent")),
   load = case_when(
     load=="high" ~ "High load",
     load=="low" ~ "Low load"
     ),
    load=factor(load,levels=c("Low load","High load")),
    logRT = log(RT)
  )

d2a.idAvgs <- d2a.analysis |>  
  group_by(id,PrimeCongruency,load) |>
  summarize(logRTavg = mean(logRT))


d2a.sums <- d2a.idAvgs|> group_by(PrimeCongruency,load) |>
  summarize(n=length(logRTavg),
            sd=sd(logRTavg,na.rm=T),
            se=sd/sqrt(n),
            logRTavg = mean(logRTavg)) 


study2afig <- ggplot(data=d2a.idAvgs, aes(x=PrimeCongruency,y=logRTavg, color=PrimeCongruency, fill=PrimeCongruency)) +
  geom_jitter(alpha=.25) +
  geom_violin(alpha=.5)+
  #geom_line(aes(group=id)) +
  geom_errorbar(data=d2a.sums, aes(ymin=logRTavg-1.96*se, ymax=logRTavg+1.96*se),width=.5) +
  geom_point(data=d2a.sums, color="Black") +
  facet_grid(~load)+
  ggtitle("Cognitive Load") +
  labs(y="Log(RT)",x="Prime Type",color="Prime Type") +
  theme_bw() +
  theme(plot.background = element_blank()
        ,panel.grid.major = element_blank()
        ,panel.grid.minor = element_blank()
        ,legend.text=element_text(size=rel(1.4))
        ,legend.position = "none",
        ,legend.title=element_text(size=rel(1.4))  
        ,axis.text.y=element_text(size=rel(1.4))
        ,axis.title.y=element_text(size=rel(1.3),vjust=.9)
        ,axis.title.x=element_text(size=rel(1.3),vjust = -.1)
        ,axis.text.x=element_text(size=rel(1.5))
        ,strip.text.x = element_text(size = rel(1.8))
        ,plot.title = element_text(hjust = 0.5,face="bold",size=rel(1.5))
  )

study2afig

ggsave(study2afig,file="../figs/study2a.jpg",dpi = 700,height = 9)

```

## Aanlyses

```{r d2a analyses}

lm2a.0 <- lmer(logRT ~ PrimeCongruency * load + primeShape + (PrimeCongruency* load|id), data = d2a.analysis)
lm2a.1 <- lmer(logRT ~ PrimeCongruency + load + primeShape + (PrimeCongruency* load|id), data = d2a.analysis)

lm2a.inter <- anova(lm2a.0,lm2a.1)
lm2a.inter # No interaction

lm2a.2 <- lmer(logRT ~ load + primeShape + (PrimeCongruency* load|id), data = d2a.analysis)
lm2a.primeCong <- anova(lm2a.1,lm2a.2)
lm2a.primeCong # large effect of prime congruence 

lm2a.3 <- lmer(logRT ~ PrimeCongruency + primeShape + (PrimeCongruency* load|id), data = d2a.analysis)
lm2a.WMload <- anova(lm2a.1,lm2a.3)
lm2a.WMload # large effect of load 

# estimated marginal means
emm2a <- emmeans(lm2a.0, ~ PrimeCongruency | load ,pbkrtest.limit = 7000)
pairs(emm2a, simple="PrimeCongruency")

```


# Study 2b

## Data cleaning

```{r 2b Cleaning}

d2b <- read.csv("Study2b_CombinedData.csv")

d2b$task[d2b$task == "marching"] <- "matching" # typo fix

d2b.training <- d2b |>
  filter(primeType == "familiarityTest" | primeType == "matchPractice" | primeType == "training", !is.na(id))

d2b.PER <- d2b |>
  filter(task == "searching", !is.na(id))

d2b <- d2b |>
  filter(task == "matching", !is.na(id)) |> 
  mutate(
    targetShape = case_when(
      grepl("AnaMatch",stimList) ~ "Unlikely",
      grepl("LocMatch",stimList) ~ "Unlikely",
      grepl("GloMatch",stimList) ~ "Likely",
      T ~ "noMatch"
    ),
    targetShape = factor(targetShape),
    primeShape = case_when(
      grepl("burst",stimList) ~ "burst",
      grepl("star",stimList) ~ "star",
      grepl("cross",stimList) ~ "cross",
     T ~ "noPrime"
    ),
   primeShape = factor(primeShape),
   primeType = case_when(
     primeType=="likely" ~ "Likely",
     primeType=="unlikely" ~ "Unlikely",
     primeType=="modal" ~ "Amodal"
     )
)

# quantile(d2b$RT, probs = seq(0, 1, 0.025)) # 95% = 3286.850; 97.5% = 4362.850

high2b <- 3286.850
# high2b <- 3500

d2b.scores <- d2b |>
  group_by(id) |>
  summarize(score = mean(correct))

d2b.scoresPER <- d2b.PER |>
  group_by(id, load) |>
  summarize(score = mean(correct))

remove <- subset(d2b.scores, subset = (d2b.scores$score < 0.9)) # create a subset of participants under threshold
remove <- bind_rows(remove, subset(d2b.scoresPER, subset = (d2b.scoresPER$score<0.85 & d2b.scoresPER$load=="low")))


d2b.cleaning <- d2b[!(d2b$id %in% remove$id),]
d2b.cleaningPER <- d2b.PER[!(d2b.PER$id %in% remove$id),]

## remove any trials from the data wherein the participant responded faster than 300 ms or slower than 3500
d2b.cleaning <- d2b.cleaning |>
  dplyr::filter(RT > 300, RT < high2b)

for (d in unique(d2b$id)){ ## loop over each participant
  # print(d)
  # print(nrow(d2b.cleaning[d2b.cleaning$id == d,]))
  # print(nrow(d2b[d2b$id == d,]))
  orig <- nrow(d2b[d2b$id == d,])
  clean <- nrow(d2b.cleaning[d2b.cleaning$id == d,])

  if((((orig - clean)/orig) > 0.1)){ ## check and see if we removed trials from a given participant
    # print("hit")
    d2b.cleaning <- d2b.cleaning[d2b.cleaning$id != d,] ## remove participant below the threshold
  }
}


d2b.cleaning$logRT <- log(d2b.cleaning$RT)

d2b.analysis <- d2b.cleaning |>
  filter(correct==1, targetType=="match")


```
## Demographics

```{r 2b demographics}

d2bdemo.clean <- d2b.cleaning |>
  group_by(id) |>
  summarize(gender = unique(gender), age = unique(age))

# length(unique(d2b.cleaning$id)) # 80 participants
# mean(d2bdemo.clean$age) # Mean age = 41.9375
# sd(d2bdemo.clean$age) # SD age = 12.12628
# length(unique(d2bdemo.clean$id[d2bdemo.clean$gender == "woman"])) # 40 identify as a woman

```

## Graph
```{r 2b graph}

d2b.analysis <- d2b.cleaning |>
  filter(correct==1, targetType=="match") |>
  mutate(
   PrimeCongruency = case_when(
    primeType == "Likely" & targetShape=="Likely" ~ "Congruent",
    primeType == "Unlikely" & targetShape=="Unlikely" ~ "Congruent",
    primeType == "Likely" & targetShape=="Unlikely" ~ "Incongruent",
    primeType == "Unlikely" & targetShape=="Likely" ~ "Incongruent",
    primeType=="Amodal" ~ "Amodal"),
   PrimeCongruency = factor(PrimeCongruency, levels=c("Congruent","Amodal","Incongruent")),
   load = case_when(
     load=="high" ~ "High load",
     load=="low" ~ "Low load"
     ),
    load=factor(load,levels=c("Low load","High load")),
    logRT = log(RT)
  )

d2b.idAvgs <- d2b.analysis |>  
  group_by(id,PrimeCongruency,load) |>
  summarize(logRTavg = mean(logRT))


d2b.sums <- d2b.idAvgs|> group_by(PrimeCongruency,load) |>
  summarize(n=length(logRTavg),
            sd=sd(logRTavg,na.rm=T),
            se=sd/sqrt(n),
            logRTavg = mean(logRTavg)) 


study2bfig <- ggplot(data=d2b.idAvgs, aes(x=PrimeCongruency,y=logRTavg, color=PrimeCongruency, fill=PrimeCongruency)) +
  geom_jitter(alpha=.25) +
  geom_violin(alpha=.5)+
  #geom_line(aes(group=id)) +
  geom_errorbar(data=d2b.sums, aes(ymin=logRTavg-1.96*se, ymax=logRTavg+1.96*se),width=.5) +
  geom_point(data=d2b.sums, color="Black") +
  facet_grid(~load)+
  ggtitle("Perceptual Load") +
  labs(y="Log(RT)",x="Prime Type",color="Prime Type") +
  theme_bw() +
  theme(plot.background = element_blank()
        ,panel.grid.major = element_blank()
        ,panel.grid.minor = element_blank()
        ,legend.text=element_text(size=rel(1.4))
        ,legend.position = "none",
        ,legend.title=element_text(size=rel(1.4))  
        ,axis.text.y=element_text(size=rel(1.4))
        ,axis.title.y=element_text(size=rel(1.3),vjust=.9)
        ,axis.title.x=element_text(size=rel(1.3),vjust = -.1)
        ,axis.text.x=element_text(size=rel(1.5))
        ,strip.text.x = element_text(size = rel(1.8))
        ,plot.title = element_text(hjust = 0.5,face="bold",size=rel(1.5))
  )

study2bfig

ggsave(study2bfig,file="../figs/study2b.jpg",dpi = 700,height = 9)

```

## Aanlyses

```{r d2b analyses}

lm2b.0 <- lmer(logRT ~ PrimeCongruency * load + primeShape + (PrimeCongruency* load|id), data = d2b.analysis)
lm2b.1 <- lmer(logRT ~ PrimeCongruency + load + primeShape + (PrimeCongruency* load|id), data = d2b.analysis)

lm2b.inter <- anova(lm2b.0,lm2b.1)
lm2b.inter # 

lm2b.2 <- lmer(logRT ~ load + primeShape + (PrimeCongruency* load|id), data = d2b.analysis)
lm2b.primeCong <- anova(lm2b.1,lm2b.2)
lm2b.primeCong # large effect of prime congruence 

lm2b.3 <- lmer(logRT ~ PrimeCongruency + primeShape + (PrimeCongruency* load|id), data = d2b.analysis)
lm2b.WMload <- anova(lm2b.1,lm2b.3)
lm2b.WMload # large effect of load 

emm2b <- emmeans(lm2b.0, ~ PrimeCongruency * load ,pbkrtest.limit = 7000)
pairs(emm2b, simple="PrimeCongruency")

```